import * as sqlite_opt from "@/utils/sqliteUtil.js";

const visualPhotoTableName = "t_acc_sum_photo";

export const requireVisualPhotoByMacnumbAndPosno = (inDmgCode, inMacType) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `SELECT * `;
		sqlStr += `from ` + visualPhotoTableName + ` where 1 = 1 `;
		sqlStr += inDmgCode ? (` and dmg_code = '` + inDmgCode + `' `) : ` `;
		sqlStr += inMacType ? (` and mac_type = ` + inMacType + ` `) : '';
		sqlStr += " order by photo_ordr ASC ";

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const addVisualPhotoByMacnumbAndPosno = (inObj) => {
	let keys = Object.keys(inObj)
	let keyStr = keys.toString()
	let valStr = ''
	keys.forEach((item, index) => {
		valStr += inObj[item] != null ? ('"' + inObj[item] + '"') : null;

		if (keys.length - 1 != index)
			valStr += ","
	})
	
	console.log("addVisualPhotoByMacnumbAndPosno")
	console.log(inObj)

	return new Promise((resolve, reject) => {
		let sqlStr = `insert into ${visualPhotoTableName} (${keyStr}) values(${valStr})`;
		console.log("addVisualPhotoByMacnumbAndPosno")
		console.log(sqlStr)
		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const updateVisualPhotoByMacnumbAndPosno = (inObj) => {
	let keys = Object.keys(inObj)
	let keyStr = keys.toString()
	let valStr = ''
	keys.forEach((item , index) => {
		valStr += item + `  =  "`;
		valStr += inObj[item] + `" ` ;
		
		if (keys.length - 1 != index)
			valStr += ","
	})

	return new Promise((resolve, reject) => {		
		let sqlStr = `update ${visualPhotoTableName} set ${valStr} 
			where dmg_code = "${inObj.dmg_code}" and photo_ordr = "${inObj.photo_ordr}" `;

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const deleteVisualPhotoByMacnumbAndPosno = (inDmgCode, inPhotoNo) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `delete from ${visualPhotoTableName} where dmg_code = ${inDmgCode} and photo_no = ${inPhotoNo} `;

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}